(function($, undefined) {

	$.fn.schoolMenu = function(options) {


		$.fn.schoolMenu.init();

		$(this).click(function() {
					$.extend($.fn.schoolMenu.defaults, options)
					$.fn.schoolMenu.$input = $(this);
					$.fn.schoolMenu.showMenu(this);
					// 阻止冒泡防止被关闭
					return false;
				});
		return this;
	};

	$.fn.schoolMenu.defaults = {
		next : function() {
		}
	};
	$.fn.schoolMenu.$schoolMenu = "";
	// 省份id
	$.fn.schoolMenu.pid = 0;
	$.fn.schoolMenu.pname = "";
	// 学校id
	$.fn.schoolMenu.sid = 0;
	$.fn.schoolMenu.schoolName = "";
	// 单个选中对象
	$.fn.schoolMenu.$input = "";

	$.fn.schoolMenu.init = function() {
		var t = this;
		// 创建或找到menu主面板
		t.$schoolMenu = $("#ffui-school-menu");
		if (t.$schoolMenu.size() == 0) {
			t.$schoolMenu = $('<div id="ffui-school-menu" class="ffui-school-menu" ><div class="ffui-school-menu-p" ></div><div class="ffui-school-menu-c" ></div></div>');
			$("body").append(t.$schoolMenu);
			// 载入省份数据
			t.loadProvince(function(jsonArr) {
						t.createProvince(jsonArr);
					});

			// 关闭
			$(document).click(function(e) {
						t.$schoolMenu.hide();
					});
			t.$schoolMenu.click(function() {
						return false;
					});
		}

	};

	// 创建省份菜单
	$.fn.schoolMenu.createProvince = function(josnArr) {
		var t = this;
		var $p = t.$schoolMenu.find("div.ffui-school-menu-p")
				.addClass("remove-bg");
		var html = "";

		$(josnArr).each(function(i, json) {
			if (json.id == 37) {
				return false;
			}
			html += '<a href="#" id="ffui-school-menu-p_' + json.id + '" >'
					+ json.name + '</a>';
		});

		$p.html(html);
		$p.find("a").click(function() {
			t.pid = $(this).attr("id").split("_")[1];
			t.pname = $(this).html();
			// 重置城市名
			t.sid = 0;
			t.schoolName = "";

			$p.find("a").removeClass("here");
			$(this).addClass("here");

			// 显示进度条
			t.$schoolMenu.find("div.ffui-school-menu-c")
					.removeClass("remove-bg");

			t.loadSchool(t.pid, function(jsonArr) {
						t.createSchool(jsonArr);
					});
			return false;
		});
	};

	// 创建学校
	$.fn.schoolMenu.createSchool = function(jsonArr) {
		var t = this;
		var $c = t.$schoolMenu.find("div.ffui-school-menu-c")
				.addClass("remove-bg");
		var html = "";
		$(jsonArr).each(function(i, json) {
			html += '<a href="#" id="ffui-school-menu-c_'
					+ json.id
					+ '" title="'
					+ json.name
					+ '" >'
					+ (json.name.length > 12 ? json.name.substring(0, 12)
							+ ".." : json.name) + '</a>';
		});
		$c.html(html).show();
		$c.find("a").click(function() {
					t.sid = $(this).attr("id").split("_")[1];
					t.schoolName = $(this).attr("title");
					$c.find("a").removeClass("here");
					$(this).addClass("here");

					t.setInfo();

					t.$schoolMenu.hide();
					// $mask.hide();
					return false;
				});
	};
	// 设置显示信息
	$.fn.schoolMenu.setInfo = function() {
		var t = this;
		t.$input.val(t.schoolName);

		t.defaults.next(t.pid, t.pname, t.sid, t.schoolName);

	};

	$.fn.schoolMenu.showMenu = function(input) {
		var t = this;
		var offset = $(input).offset();

		t.$schoolMenu.css({
					top : offset.top + $(input).outerHeight(true),
					left : offset.left,
					display : "block"
				});
	};

	// 载入地区数据
	$.fn.schoolMenu.loadProvince = function(next) {
		$.get('/v2/js/ffui/data/province/province.js', function(data) {
					if (next) {
						var json = $.parseJSON(data);
						next(json);
					}
				});
	};
	// 载入学校数据
	$.fn.schoolMenu.loadSchool = function(id, next) {
		$.get('/v2/js/ffui/data/dx/dx_' + id + ".js?", function(data) {
					if (next) {
						var json = $.parseJSON(data);
						next(json);
					}
				});
	};
})(jQuery);
